package com.qf.servlet;

import com.qf.entity.User;
import com.qf.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录的Servlet
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    /**
     * 用户的业务对象
     */
    private UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //直接调用dao层查询用户信息
        User user = userService.login(username, password);

        //判断是否登录成功
        if (user != null) {
            //登录成功
            //存入session - 用户信息
            req.getSession().setAttribute("login_user", user);
            //跳转到首页
            resp.sendRedirect("index.jsp");
        } else {
            //登录失败
            resp.sendRedirect("login.jsp?status=1");
        }
    }
}
